package com.psp.park.service.impl;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.northend.util.MD5Util;
import com.northend.util.NumUtil;
import com.northend.util.StringUtil;
import com.psp.park.cache.dao.UserCacheDao;
import com.psp.park.model.Code;
import com.psp.park.model.ParkUserBean;
import com.psp.park.model.UserLoginLogBean;
import com.psp.park.persist.dao.UserDao;
import com.psp.park.service.UserService;
import com.psp.park.service.exception.ServiceException;

@Service
public class UserServiceImpl implements UserService {
	Logger logger = Logger.getLogger(UserServiceImpl.class);

	@Autowired
	UserDao userImpl;

	@Autowired
	UserCacheDao userCacheImpl;

	@Transactional(rollbackFor = Exception.class)
	@Override
	public ParkUserBean login(String sessionId, String account, String pwd, String vcode, String device, String ip) {
		Code code = userCacheImpl.getLoginCode(account);
		if (code == null) {
			code = new Code();
			code.setNum(0);
			userCacheImpl.setLoginCode(account, code);
		} else if (code.getNum() > 4) {
			if (StringUtil.isEmpty(code.getCode())) {
				throw new ServiceException("imgcode_is_cross");
			}
			if (StringUtil.isEmpty(vcode) || !code.getCode().equals(vcode.toUpperCase())) {
				throw new ServiceException("imgcode_is_error");
			}
		}
		ParkUserBean user = null;
		user = userImpl.selectOneByAccount(account);
		if (user == null) {
			throw new ServiceException("object_is_not_exist", new Object[] { "用户" });
		}

		pwd = MD5Util.sha256Hex(pwd);
		if (!pwd.equals(user.getPassword())) {
			code.setNum(code.getNum() + 1);
			userCacheImpl.setLoginCode(account, code);
			throw new ServiceException("user_password_is_error");
		}

		if (NumUtil.toInt(user.getState(), 0) == 2) {
			throw new ServiceException("user_is_not_active");
		}

		userCacheImpl.setUserSession(sessionId, user.getPuid(), 86400L);
		boolean flag = userImpl.updateLoginTime(user.getPuid()) > 0;
		if (flag) {
			UserLoginLogBean loginLog = new UserLoginLogBean();
			loginLog.setDevice(device);
			loginLog.setLoginIp(ip);
			loginLog.setUid(user.getPuid());
			loginLog.setOstate(1);// 1 登录 2注册
			flag = userImpl.insertLoginLog(loginLog) > 0;
			if (!flag) {
				throw new ServiceException("fail");
			}
			code = new Code();
			code.setNum(0);
			userCacheImpl.setLoginCode(account, code);
		}

		return user;
	}

	@Override
	public ParkUserBean existUser(int type, String username) {
		ParkUserBean user = userImpl.selectOneByAccount(username);
		if (type == 1) {
			if (user == null) {
				throw new ServiceException("object_is_not_exist", new Object[] { "用户" });
			}
		} else if ((type == 2) && (user != null)) {
			throw new ServiceException("object_is_exist", new Object[] { "用户" });
		}
		return user;
	}

	@Override
	public ParkUserBean existUser(String sessionId) {
		String uid = userCacheImpl.getUidBySession(sessionId);
		if (uid == null) {
			uid = "0e56b63a963449bca96bcef6cb822123";
		}
		return userImpl.selectOne(uid);
	}

	@Override
	public ParkUserBean getUser(String userId) {
		if (userId == null) {
			userId = "0e56b63a963449bca96bcef6cb822123";
		}
		return userImpl.selectOne(userId);
	}

	public void checkImgCode(String userName, String i) {
		Code imgCode = userCacheImpl.getImgCode(userName);
		logger.info("the imgcode is : " + imgCode);
		if (imgCode == null) {
			throw new ServiceException("imgcode_is_expire");
		}
		i = i.toUpperCase();
		if ((StringUtil.isEmpty(i)) || (!i.equals(imgCode.getCode()))) {
			logger.info("set imgcode is : " + imgCode);
			throw new ServiceException("imgcode_is_error");
		}
	}

}
